package com.bw.unit3.view.activity

import android.content.Intent
import android.os.CountDownTimer
import android.widget.ImageView
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bw.unit3.Const
import com.bw.unit3.R
import com.bw.unit3.base.BaseActivity
import com.bw.unit3.databinding.ActivityWelcomeBinding
import com.bw.unit3.viewmodel.WelcomeViewModel

/**
 *
 * @ClassName: WelcomeActivity
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/13 2:51 PM
 * @Version: 1.0
 */

class WelcomeActivity :
    BaseActivity<WelcomeViewModel, ActivityWelcomeBinding>(R.layout.activity_welcome) {

    private var countDownTimer: CountDownTimer? = null
    override fun initData() {
        viewModel.bannerLiveData.observe(this) {
            //设置数据源
            dataBinding.banner.setData(it.data, null)
            //自动播放
//            dataBinding.banner.setAutoPlayAble(true)
//            dataBinding.banner.setAutoPlayTime(1500)
//            dataBinding.banner.setmAdapter(object : XBannerAdapter {
//                override fun loadBanner(banner: XBanner?, model: Any?, view: View?, position: Int) {
//                    Glide.with(this@WelcomeActivity)
//                        .load(it.data[position].imagePath)
//                        .into(view as ImageView)
//                }
//            })
            dataBinding.banner.setmAdapter { _, _, view, position ->
                Glide.with(this@WelcomeActivity)
                    .load(it.data[position].imagePath)
                    .into(view as ImageView)
            }
        }
    }

    override fun initView() {
        //点击跳过
        dataBinding.tvStip.setOnClickListener {
            startActivity(Intent(this@WelcomeActivity, LoginActivity::class.java))
        }

        //获取banner
        viewModel.getBanner()

        //倒计时
        countDownTimer = object : CountDownTimer(3000, 1000) {
            override fun onTick(p0: Long) {
                //每间隔1s执行一次
                dataBinding.tvStip.text = "${p0 / 1000}S后跳转"
            }

            override fun onFinish() {
                //执行完成
                ARouter.getInstance()
                    .build(Const.PATH_LOGIN)
                    .withString("userName","zhangsan")
                    .navigation()
//                startActivity(Intent(this@WelcomeActivity, LoginActivity::class.java))
            }
        }
        //开始倒计时
        countDownTimer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer?.cancel()
    }
}